const db = wx.cloud.database()
var util = require('../util/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isSend: true,
    choose:true,
    product: '选择商品',
    saleCntStr: '选择数量',
    giftCntStr: '赠送数量',
    showSaleCnt: true,
    showGiftCnt:true,
    customPrice: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    var that = this
    wx.getStorage({
      key: 'openid',
      success: function (res) {
        that.setData({
          openid: res.data
        })
      },
    })
   
    const res = await wx.cloud.callContainer({
      "config": {
        "env": "prod-0gtdyzqaeb5d3e99"
      },
      "path": "/biz/product/list?shopId=1",
      "header": {
        "X-WX-SERVICE": "springboot-xgga"
      },
      "method": "GET"
    })
    console.log("加载商品")
    that.setData({
      dataList: res.data.data
    })
  },
  showChangePrice(event) {
    this.setData({
      customPrice: !this.data.customPrice,
      showSaleCnt: this.data.customPrice,
    })
  },
  changeCashierType(event) {
    console.log(event.currentTarget.dataset.cnt)
    this.setData({
      cashierType: event.currentTarget.dataset.cnt
    })
  },
  showProduct(event) {
    this.setData({
      choose:true
    })
  },
  showSaleCnt(event) {
    this.setData({
      showSaleCnt:!this.data.showSaleCnt,
      customPrice: this.data.showSaleCnt
    })
  },
  showGift(event) {
    this.setData({
      showGift:true
    })
  },
  chooseProduct(event) {
    console.log(event)
    this.setData({
      productId: event.currentTarget.dataset.id,
      choose:false,
      product:event.currentTarget.dataset.name,
      price: parseFloat(event.currentTarget.dataset.price)
    })
  },
  changeCnt(event) {
    var cnt = parseFloat(event.currentTarget.dataset.cnt)
    var saleCnt = parseFloat(this.data.saleCnt)
    var res = cnt + saleCnt
    if(res <= 0) {
      wx.showToast({
        icon: "none",
        title: '再减就没了'
      })
      return
    }
    this.setData({
      saleCnt: res,
      saleCntStr: '买'+res + '斤',
      income: parseFloat(this.data.price) * parseFloat(res)
    })
    this.calcDefaultGift(event)
  },
  chooseCnt(event) {
    this.setData({
      saleCnt: event.currentTarget.dataset.cnt,
      saleCntStr:  '买'+event.currentTarget.dataset.cnt + '斤',
      income: parseFloat(this.data.price) * parseFloat(event.currentTarget.dataset.cnt)
    })
    console.log(this.data.income)
    this.calcDefaultGift(event)
  },
  calcDefaultGift(event) {
    var saleCnt = parseInt(this.data.saleCnt)
    var giftCnt = parseInt(saleCnt / 5)
    this.setData({
      giftCnt: giftCnt,
      giftCntStr: '送' + giftCnt + '斤'
    })
  },
  chooseGift(event) {

    var giftTag = event.currentTarget.dataset.gift
    var customGift = parseFloat(this.data.giftCnt)
    var customStr = this.data.giftCntStr
    if(giftTag == '1') {
        customGift = 0
        customStr = '不送酒'
    }else {
      var double = event.currentTarget.dataset.double
      if(double == '1') {
        customGift = this.data.saleCnt
      }else {
        var calcGift = parseFloat(event.currentTarget.dataset.cnt)
        customGift += calcGift
      
      }
      if(customGift == 0) {
        customStr = '不送酒'
      }else{
        customStr = '送' + customGift + '斤'
      }
    }
    this.setData({
      giftCnt: customGift,
      giftCntStr: customStr
    })
  },
  //打开弹窗
  send: function () {
    var that = this
    wx.getStorage({
      key: 'login',
      success: function (res) {
        if (res.data) {
          that.setData({
            isSend: true
          })
        } else {
          wx.showToast({
            icon: "none",
            title: '你还未登录'
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          icon: "none",
          title: '你还未登录'
        })
      }
    })
  },
  // 关闭弹窗
  close: function () {
    this.setData({
      isSend: false
    })
  },
  //上传数据
  publish: async function () {
    let productId = this.data.productId
    if (!productId) {
      wx.showToast({
        icon: "none",
        title: '请选择商品'
      })
      return
    }
    wx.showLoading({
      title: '录入中...',
    })

    const res = await wx.cloud.callContainer({
      "config": {
        "env": "prod-0gtdyzqaeb5d3e99"
      },
      "path": "/biz/fast-fill",
      "header": {
        "X-WX-SERVICE": "springboot-xgga"
      },
      "method": "POST",
      "data":{
        productId: productId,
        shopId: 1
      }
    })
    if(res.data.code == '0') {
        wx.hideLoading()
        wx.showToast({
          title: '录入成功',
        })
        console.log('发布成功', res)
        this.setData({
          product: '选择商品',
          productId: null
        })

      } else {
        wx.hideLoading()
        wx.showToast({
          icon: 'none',
          title: '网络不给力....'
        })
        console.error('发布失败', res)
      }
    

  },
  delete: function (e) {
    var info = e.currentTarget.dataset.t
    console.log(info)
    db.collection('biaobai').doc(info._id).remove({
      success: function (res) {
        console.log(res.data)
        wx.showToast({
          icon: 'success',
          title: '删除成功',
        })
      }
    })
    this.onLoad()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})